package it.polimi.swim.session;

import java.util.ArrayList;
import java.util.List;

import it.polimi.swim.entity.HelpRequest;
import it.polimi.swim.entity.Skill;
import it.polimi.swim.entity.SystemUser;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.jboss.ejb3.annotation.RemoteBinding;

@Stateless
@RemoteBinding(jndiBinding = SystemUserManagerBean.REMOTE)
public class SystemUserManagerBean implements SystemUserManagerBeanRemote {

	public static final String REMOTE = "SystemUserManagerBean/remote";

	@PersistenceContext(unitName = "SWIMv2")
	private EntityManager em;

	public SystemUserManagerBean() {

	}

	@Override
	public SystemUser createUser(String name, String surname, String city,
			String mail, String psw, String day, String month, String year) {

		//String dateString = new String(year + "-" + month + "-" + day);
		SystemUser user = new SystemUser();

		user.setName(name);
		user.setSurname(surname);
		user.setMail(mail);
		user.setCity(city);
		user.setPsw(psw);
		user.setType("User");
		
		em.persist(user);

		return user;
	}

	@Override
	public SystemUser editUser(String name, String surname, String city,
			String mail, String psw, String day, String month, String year) {

		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public SystemUser validateLogin(String mail, String psw) {

		Query q = null;

		List<SystemUser> userList = new ArrayList<SystemUser>();

		q = em.createNamedQuery("SystemUser.validateLogin");
		q.setParameter("mail", mail);
		q.setParameter("psw", psw);

		System.out.println("Faccio la query eh!");

		userList = (ArrayList<SystemUser>) q.getResultList();

		System.out.println("Ho fatto la query");

		System.out.println("Dimensione:" + userList.size());

		if (userList.size() == 1) {
			if ((userList.get(0).getMail().equals(mail)&& (userList.get(0).getPsw().equals(psw)))) {

				System.out.println("Ecco cosa ho trovato nel DB:");
				System.out.println("Mail:" + userList.get(0).getMail());
				System.out.println("login ok!");
				return userList.get(0);
			}
		}

		return null;
	}

	@Override
	public List<SystemUser> searchUserBySkill(int idSkill) {
		Query q;
		Skill s;
		q = em.createNamedQuery("Skill.getSkillById");
		q.setParameter("id", idSkill);
		s = (Skill) q.getSingleResult();
		q = em.createNamedQuery("SystemUser.getUserWithSkill");
		q.setParameter("skill", idSkill);
		List<SystemUser> users = q.getResultList();
		return users;
	}

	@Override
	public List<SystemUser> searchUserByName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SystemUser> showFriends(SystemUser user) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public SystemUser searchUserById(int id) {
		Query q = null;
		SystemUser user;
		
		q = em.createNamedQuery("SystemUser.getUserById");
		q.setParameter("id", id);
		user = (SystemUser) q.getSingleResult();
		return user;
	}

	@Override
	public void searchUserBySkill(int idSkill, String set) {
		// TODO Auto-generated method stub
		
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<SystemUser> searchUserByName(String name, String surname) {
		List<SystemUser> userList = new ArrayList<SystemUser>();
		Query q = null;
		q = em.createNamedQuery("SystemUser.getUserByName");
		q.setParameter("name", name);
		q.setParameter("surname", surname);
		userList = (List<SystemUser>) q.getResultList();
		System.out.println(userList.size());
		if(userList.size()>0){
			System.out.println(userList.get(0).getName() +" " + userList.get(0).getSurname());
		}
		return userList;
	}

	@Override
	public void editOldUser(int id, String name, String surname, String psw,
			String city) {
		Query q = null;
		q = em.createNamedQuery("SystemUser.getUserById");
		q.setParameter("id", id);
		SystemUser user = (SystemUser) q.getSingleResult();
		user.setCity(city);
		user.setName(name);
		user.setSurname(surname);
		user.setPsw(psw);
		
		em.persist(user);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void addSkill(String mail, String psw, int idSkill) {
		Query q = null;
		q = em.createNamedQuery("SystemUser.validateLogin");
		q.setParameter("mail", mail);
		q.setParameter("psw", psw);
		SystemUser user = (SystemUser) q.getSingleResult();
		System.out.println("userok");
		q = null;
		q = em.createNamedQuery("Skill.getSkillById");
		q.setParameter("id", idSkill);
		Skill s = (Skill) q.getSingleResult();
		s.getUsers().add(user);
		user.getSkill().add(s);
		em.persist(s);
		em.persist(user);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<HelpRequest> getUserCompletedHelp(int id) {
		Query q = null;
		q = em.createNamedQuery("SystemUser.getUserCompletedHelp");
		q.setParameter("id", id);
		System.out.print("id!!"+id);
		return q.getResultList();
	}

	@Override
	public int controlUserDuplicate(String mail) {
		Query q = null;
		q = em.createNamedQuery("SystemUser.duplicateUser");
		q.setParameter("mail", mail);
		return q.getResultList().size();
	}

	@Override
	public void createAdmin() {
		SystemUser user = new SystemUser();

		user.setName("admin");
		user.setSurname("admin");
		user.setMail("admin");
		user.setPsw("admin");
		user.setCity("admin");
		user.setType("Admin");
		
		em.persist(user);
		
	}
}
